Imports Clo = Enhancements.Core.Logging
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml
Imports Vbn = Microsoft.VisualBasic

Namespace Enhancements.Basic.Expressions.Values
    ''' <summary>Represents a token extracted from an expression that has a boolean value.</summary>
    ''' <remarks>The representations allowed are the defined by the TrueValueRepresentation and FalseValueRepresentation properties with case-insensitivity.</remarks>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class [Boolean]
        Inherits Value

        Private Sub New(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _expressionEndIndex As Sys.Int32, ByVal _value As System.Boolean)
            MyBase.New(_expression, _expressionBeginIndex, _expressionEndIndex, _value)
        End Sub

        ''' <summary>The associated framework type used for the token value storage.</summary>
        Public Shared ReadOnly Property UnderlyingType() As Sys.Type
            Get
                Dim _returnValue As Sys.Type = GetType(System.Boolean)

                Return _returnValue
            End Get
        End Property

        ''' <summary>The True value correct representation in an expression.</summary>
        Public Shared ReadOnly Property TrueValueRepresentation() As System.String
            Get
                Dim _returnValue As System.String = "True"

                Return _returnValue
            End Get
        End Property

        ''' <summary>The False value correct representation in an expression.</summary>
        Public Shared ReadOnly Property FalseValueRepresentation() As System.String
            Get
                Dim _returnValue As System.String = "False"

                Return _returnValue
            End Get
        End Property

        Private Shared Function __Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32) As Sys.Object
            Dim _returnValue As Sys.Object = Nothing
            If _expression.IsNullNot("_expression", False) AndAlso _expression.IsEmptyNot("_expression", False) AndAlso _expressionBeginIndex.IsInRange("_expressionBeginIndex", 0, _expression.Length - 1, False) Then
                If ___Texts.StartsWith(_expression.Substring(_expressionBeginIndex), FalseValueRepresentation) Then
                    _returnValue = New [Boolean](_expression, _expressionBeginIndex, _expressionBeginIndex + FalseValueRepresentation.Length - 1, False)
                End If
                If ___Texts.StartsWith(_expression.Substring(_expressionBeginIndex), TrueValueRepresentation) Then
                    _returnValue = New [Boolean](_expression, _expressionBeginIndex, _expressionBeginIndex + TrueValueRepresentation.Length - 1, True)
                End If
            End If
            If Assert.IsNull(_returnValue, "_returnValue", False) Then
                _returnValue = BooleanParseException.___Create(_expression, _expressionBeginIndex)
            End If

            Return _returnValue
        End Function

        ''' <summary>Parses an expression for a boolean value starting by a begin index.</summary>
        ''' <param name="_expression">Expression to parse.</param>
        ''' <param name="_expressionBeginIndex">Expression begin index to use.</param>
        ''' <exception cref="BooleanParseException">The operation did not produce a valid token.</exception>
        ''' <returns> </returns>
        Public Overloads Shared Function Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32) As [Boolean]
            Dim _returnValue As Sys.Object = __Parse(_expression, _expressionBeginIndex)
            If Assert.IsOfType(_returnValue, "_returnValue", GetType(BooleanParseException), False) Then
                BooleanParseException.___Throw(CType(_returnValue, BooleanParseException))
            End If

            Return CType(_returnValue, [Boolean])
        End Function

        ''' <summary>Tries to parse an expression for a boolean value starting by a begin index.</summary>
        ''' <param name="_expression">Expression to parse.</param>
        ''' <param name="_expressionBeginIndex">Expression begin index to use.</param>
        ''' <param name="_result">The result of the operation if possible. If not, null is returned.</param>
        ''' <returns> </returns>
        Public Overloads Shared Function TryParse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByRef _result As [Boolean]) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Dim _dummy As Sys.Object = __Parse(_expression, _expressionBeginIndex)
            If Assert.IsOfType(_dummy, "_dummy", GetType(BooleanParseException), False) Then
                _result = Nothing
                _returnValue = False
            Else
                _result = CType(_dummy, [Boolean])
                _returnValue = True
            End If

            Return _returnValue
        End Function

        Friend Overloads Shared Function ___AssertIsValidValue(ByVal _value As Sys.Object) As System.Boolean
            Dim _returnValue As System.Boolean = False
            If Assert.IsNullNot(_value, "_value", False) Then
                If Assert.IsOfType(_value, "_value", [Boolean].UnderlyingType, False) Then
                    _returnValue = True
                End If
            End If

            Return _returnValue
        End Function

        ''' <summary>Determines the token's correct representation for use in an expression.</summary>
        ''' <returns> </returns>
        Public Overrides Function ToRepresentation() As System.String
            Dim _returnValue As System.String = __ToRepresentation(CType(Value, System.Boolean))

            Return _returnValue
        End Function

        ''' <summary>Determines the correct representation of a boolean value for use in an expression.</summary>
        ''' <exception cref="BooleanRepresentationException">The operation did not produce a valid representation because of the input value nature.</exception>
        ''' <returns> </returns>
        Public Overloads Shared Function ToRepresentation(ByVal _value As System.Boolean) As System.String
            If Not ___AssertIsValidValue(_value) Then
                BooleanRepresentationException.___Throw(_value)
            End If

            Dim _returnValue As System.String = __ToRepresentation(_value)

            Return _returnValue
        End Function

        Private Shared Function __ToRepresentation(ByVal _value As System.Boolean) As System.String
            Dim _returnValue As System.String = Nothing
            Select Case _value
                Case True
                    _returnValue = [Boolean].TrueValueRepresentation
                Case False
                    _returnValue = [Boolean].FalseValueRepresentation
            End Select

            Return _returnValue
        End Function

    End Class
End Namespace
